﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace SortTestser
{
    class Program
    {
        private static string executableFile;

        private static void InitTest(int count, string filename)
        {
            StreamWriter input = new StreamWriter(filename);
            Random rand = new Random();
            for (int i = 0; i < count; i++)
            {
                int rnd;
                rnd = rand.Next(1, 10000000);
                input.Write(rnd.ToString());
                if (i != count - 1)
                {
                    input.Write(" ");
                }
            }
            input.Close();
        }

        private static int Execute(string inputFileName, string outputFileName)
        {
            Process p = new Process();
            p.StartInfo.FileName = executableFile;
            p.StartInfo.Arguments = inputFileName + " " + outputFileName;
            p.Start();
            p.WaitForExit();
            int exitCode = p.ExitCode;
            p.Close();
            return exitCode;
        }

        private static bool SortCorrectTest()
        {
            string outputFileName = "output";

            InitTest(100, "correct");
            if (Execute("correct", outputFileName) != 0 || !File.Exists("output"))
                return false;
            StreamReader input = new StreamReader(outputFileName);
            uint counter = 0, prev = 0;
            while (!input.EndOfStream)
            {
                prev = counter;
                while (input.Peek() != ' ' && !input.EndOfStream)
                {
                    counter *= 10;
                    counter += (uint)(input.Read() - '0');
                }
                input.Read();
                if (counter < prev)
                    return false;
                counter = 0;
            }

            input.Close();
            File.Delete("correct");
            File.Delete(outputFileName);
            return true;
        }

        private static bool EmptyFileTest()
        {
            InitTest(0, "empty");
            if (Execute("empty", "output") != 0)
                return false;
            File.Delete("empty");
            return true;
        }

        private static bool FileTest(int count)
        {
            InitTest(count, "input");
            if (Execute("input", "output") != 0)
                return false;
            File.Delete("input");
            if (File.Exists("output"))
                File.Delete("output");
            return true;
        }

        private static bool WrongFormatTest()
        {
            StreamWriter input = new StreamWriter("wrong");
            input.Write("abcd");
            input.Close();
            if (Execute("wrong", "output") != 0)
                return false;
            File.Delete("wrong");
            if (File.Exists("output"))
                File.Delete("output");
            return true;
        }

        private static bool BigNumberTest()
        {
            StreamWriter input = new StreamWriter("bignumber");
            input.Write(Int64.MaxValue.ToString() + Int64.MaxValue.ToString());
            input.Close();
            if (Execute("bignumber", "output") != 0)
                return false;
            File.Delete("bignumber");
            if (File.Exists("output"))
                File.Delete("output");
            return true;
        }

        static void Main(string[] args)
        {
            executableFile = args[0];
            if (!SortCorrectTest())
                Console.WriteLine("Sorting isn't correct");
            if (!EmptyFileTest())
                Console.WriteLine("Empty file causes exception");
            if (!BigNumberTest())
                Console.WriteLine("Big numbers cause exceptions");
            if (!WrongFormatTest())
                Console.WriteLine("Wrong format causes exception");
            if (!FileTest(1300000))
                Console.WriteLine("Failed 10 Mb test");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
    }
}
